<template>
  <div class="login-container video-background">

    <video ref="video" autoplay loop muted>
      <source src="https://slwl-admin.itheima.net/static/media/video.3cf57b17.mp4" type="video/mp4">
    </video>
    <!-- other content goes here -->

    <el-form ref="Form" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">

      <div class="title-container">
        <!-- <h3 class="title">Login Foaaarm</h3> -->
        <img src="https://slwl-admin.itheima.net/static/img/logo1.33ef7fa0.png" alt="">
      </div>

      <el-form-item prop="username">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="username"
          v-model="loginForm.account"
          placeholder="账号"
          name="username"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="密码"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>

      <el-form-item prop="yname" class="qqqqq">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          ref="yname"
          v-model="loginForm.code"
          placeholder="验证码"
          name="yname"
          type="text"
          tabindex="3"
          auto-complete="on"
        />
        <!-- <img class="code-image" src="../" alt=""> -->
        <img :src="imageUrl" alt="" class="yanzm" @click="yanzm">
      </el-form-item>

      <el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;" @click.native.prevent="handleLogin"><b>登录</b></el-button>

      <div class="tips">
        <!-- <span style="margin-right:20px;">username: admin</span>
        <span> password: any</span> -->
        <span>
          仅用于IT培训教学使用，为保障您的个人信息安全，请勿向平台录入任何个人敏感信息（如手机号、身份证号等）！</span>
      </div>

    </el-form>
    <span class="qqq">江苏传智播客教育科技股份有限公司 版权所有Copyright 2006-2022 All Rights Reserved 苏ICP备16007882号-11</span>

  </div>
</template>

<script>
// import { validUsername } from '@/utils/validate'
import axios from 'axios'
export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        account: 'shenlingadmin',
        password: '123456',
        code: ''
      },
      loginRules: {
        username: [{ min: 1, max: 16, message: '账号长度应该为1-10位之间', trigger: ['blur', 'change'] }],
        password: [{ required: true, message: '密码不能为空', trigger: 'blur' }, { min: 1, max: 16, message: '密码长度应该为1-10位之间', trigger: ['blur', 'change'] }],
        yname: [{ min: 1, max: 2, message: '验证码长度应该为1-2位之间', trigger: ['blur', 'change'] }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      imageUrl: ''
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  mounted() {
    this.$refs.video.play()
  },
  created() {
    this.yanzm()
  },
  methods: {
    // 验证码 和 key
    async yanzm() {
      // 验证码
    // 1. 随机数
      const random = Math.random() * 9 + 1
      this.loginForm.key = random
      // 2. 发请求获取验证码
      // const res = await verify(random)
      const res = await axios({
        url: 'https://slwl-api.itheima.net/manager/captcha',
        method: 'get',
        // 表示返回的是一个二进制数据块
        responseType: 'blob',
        params: {
          key: random
        }
      })
      // console.log(res)
      // 3. 渲染到
      // 转成 base64
      const reader = new FileReader()
      reader.readAsDataURL(res.data)
      reader.onloadend = () => {
        this.imageUrl = reader.result
      // console.log(this.imageUrl)
      }
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    async  dologin() {
      console.log('做具体的登录操作（发请求）')

      // 将数据传给仓库处理
      await this.$store.dispatch('user/login', this.loginForm)
      this.$message.success('登录成功')
      this.$router.push('/')
    },
    handleLogin() {
      this.$refs.Form.validate((valid) => {
        valid && this.dologin()
      })
    }

  }

}

</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:white;
$light_gray:#fff;
$cursor: black;
.video-background {
  position: relative;
  height: 100vh;
  background: transparent;
}

.video-background video {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.qqq{
  position: absolute;
  top:650px;
  left: 400px;
  width: 100%;
  height: 100%;
  color: white;
}

// @supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
//   .login-container .el-input input {
//     color: $cursor;
//   }
// }

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: black;
      height: 47px;
      caret-color: black;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid #dcdfe6;
    background: white;
    border-radius: 5px;
    color: #454545;
  }
  .el-button{
     background:#e15536;
     border: 1px solid #e15536;

  }
}
</style>

<style lang="scss" scoped>

$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  min-height: 100%;
  width: 100%;

  overflow: hidden;

  .login-form {
    position: relative;
    width: 450px;
    height: 490px;
    max-width: 100%;
    padding: 60px 35px 60px 35px;
    margin:   0 auto;
    overflow: hidden;
    background-color: #ffffff;
margin-top:100px;
border-radius: 10px;
  }

  .tips {
    font-size: 14px;
    color: #fe7a33;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
  }

  .title-container {
    position: relative;

img{
  width: 200px;

  margin: 0 auto;
  display: flex;
margin-bottom: 20px;
}
    .title {
      font-size: 26px;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
// .qqqqq{
//    position: relative;
// }
.yanzm {
  background-color: pink;
  width: 72px;
  height: 32px;
  position:absolute ;
   right: 10px;
    top: 17px;
}
</style>
